<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Api\System\CoreController;
use Illuminate\Http\Request;
use Aliyun\SmsDemo;
use App\Http\Models\User\UserBaseModel;


class UserBaseController extends ApiController
{
    public function __construct()
    {
        parent::__construct();
    }


    public function do_login(Request $request)
    {
        if(empty($request->input('user_name'))){
            return self::response(null,'-1','用户名为空');
        }

        if(empty($request->input('user_password'))){
            return self::response(null,'-2','密码为空');
        }

        $ret = UserBaseModel::check_user($request->input('user_name'),$request->input('user_password'));

        if ($ret == 2)
        {
            return self::response([],40002,'登录失败,用户未注册');
        }

        if ($ret == 3)
        {
            return self::response([],40003,'用户名或密码填写错误');
        }

        //更新token
        $user   = UserBaseModel::where('user_name',$request->input('user_name'))->select()->first();
        $Token  = UserBaseModel::updateToken($user->id);
        $user['user_token']  = $Token;

        //更新登录状态
        $user['user_nickname'] = empty($user['user_nickname'])?'':$user['user_nickname'];
        CoreController::set_user_login_status($Token,$user);
        $user['mac'] = '';
        if ($user['is_liver']==1)
        {
            $liveInfo = LiveModel::getLiverRoomInfo($user['id']);
            if (!empty($liveInfo['mac'])){
                $user['mac'] = $liveInfo['mac'];
            }
        }

        return self::response($user);
    }


    public function do_reg(Request $request)
    {
        if (empty($request->input('user_password')))
        {
            return self::response([],40001,'缺少参数:user_password');
        }

        if (empty($request->input('user_name')))
        {
            return self::response([],40001,'缺少参数:user_name');
        }

        //检查是否注册过
        $check_ret = UserBaseModel::check_user($request->input('user_name'));

        if ($check_ret == 1)
        {
            return self::response([],40001,'注册失败,用户已注册');
        }

        $reg_ret = UserBaseModel::new_user([
            'user_name'     => $request->input('user_name'),
            'user_password' => $request->input('user_password'),
            'user_telphone' => $request->input('reg_from') == 'telphone' ? $request->input('user_name') : ''
        ]);
//
        $Token                   = UserBaseModel::updateToken($reg_ret->id);
        $InviteCode              = UserBaseModel::updateInviteCode($reg_ret->id);
        $reg_ret['user_token']   = $Token;
        $reg_ret['Invite_code']  = $InviteCode;
        //为用户绑定注册随心播account
        if ($reg_ret)
        {
            CoreController::set_user_login_status($Token,$reg_ret);
            return self::response($reg_ret);
        }
        else
        {
            return self::response([],40002,'注册失败');
        }
    }

    public function get_sms_code(Request $request)
    {
        if (!$request->input('phone'))
        {
            return self::response([],'40001','缺少必传参数');
        }
        return self::response(CoreController::sendCode($request->input('phone')));
    }
    public function check_sms_code(Request $request)
    {
        $ret = CoreController::check_sms_code($request->input('phone'),$request->input('code'));
        return $ret ? self::response(['msg'=>'验证码校验正确','type'=>'ok']) : self::response(['msg'=>'验证码校验错误','type'=>'failed']);
    }


    public function search_user(Request $request)
    {
        if (!$request->input('user_nickname'))
        {
            return self::response([],40001,'少传参数user_nickname');
        }
        $data = UserBaseModel::where('user_nickname','like',"%".$request->input('user_nickname')."%")->select("id","user_nickname")->get();
        return self::response($data);
    }

    public static function _add_message($request)
    {
        $psh = new PushController();
        $ret = $psh->pushToUid($request->input('title'),$request->input('uid'));
        if ($ret['result'] == 'ok')
        {
            $msg = new MessageModel();
            $msg->uid = $request->input('uid');
            $msg->message = $request->input('message');
            $msg->title = $request->input('title');
            $msg->is_read = 0;
            $msg->save();
        }
        else
        {
            return false;
        }
        return true;
    }
    public function add_message(Request $request)
    {
        if (!$request->input('message'))
        {
            return self::response([],40001,'缺少message');
        }
        if (!$request->input('uid'))
        {
            return self::response([],40001,'缺少uid');
        }
        if (!$request->input('title'))
        {
            return self::response([],40001,'缺少title');
        }
        $ret = $this->_add_message($request);
        if ($ret)
        {
            return self::response();
        }
        else
        {
            return self::response([],40002,'发送推送失败');
        }


    }


    public function forget_password(Request $request)
    {
        if (!$request->input('user_name') || !$request->input('user_password'))
        {
            return self::response([], 40001, '请输入用户名和密码');
        }

        if ( !$request->input('re_password'))
        {
            return self::response([], 40001, '请再次输入密码');
        }

        if ($request->input('user_password') != $request->input('re_password'))
        {
            return self::response([], 40011, '两次输入密码不相等');
        }

        if (!$request->input('code'))
        {
            return self::response([], 40011, '请输入验证码');
        }


        $ret = CoreController::check_sms_code($request->input('user_name'),$request->input('code'));

        if (empty($ret)){
            return self::response('验证码错误');
        }

        $user_info = UserBaseModel::where('user_name', $request->input('user_name'))->select()->first();

        if (!$user_info->id)
        {
            return self::response([], 40003, '用户不存在');
        }

        $res =  $this->_change_password($user_info,$request->input('user_password'));

        if ($res==false){
            return  self::response([],40000,'修改密码失败');
        }

        $user   = UserBaseModel::where('user_name', $request->input('user_name'))->select()->first();
        #修改成功后 获取登陆信息

        $Token  = UserBaseModel::updateToken($user->id);
        $user['user_token']  = $Token;

        //更新登录状态
        $user['user_nickname'] = empty($user['user_nickname'])?'':$user['user_nickname'];
        CoreController::set_user_login_status($Token,$user);
        return  self::response($user,200,'修改密码成功');
    }

    public function _change_password(UserBaseModel $user_info,string $password)
    {
        $newPass                    = ApiController::mkPass($password,$user_info->user_salt);
        $user_info->user_password   = $newPass;
        return $user_info->save();
    }

    public function change_telphone(Request $request)
    {
        if (!$request->input('user_telphone'))
        {
            return self::response([], 40001, '缺少参数');
        }
        $user = UserBaseModel::where('user_token',$request->input('token'))->select()->first();
        if (!$user->id)
        {
            return self::response([], 40003, 'token失效,请重新登录');
        }
        $user->user_telphone = $request->input('user_telphone');
        $user->user_telphone = $request->input('user_telphone');
        $res = $user->save();
        return $res ? self::response([],200,'修改手机号成功') : self::response([],40000,'修改手机号失败');
    }

    /**
     * 用户中心修改密码
     */
    public function change_password(Request $request)
    {
        if (!$request->input('password') || !$request->input('repassword'))
        {
            return self::response([], 40001, '缺少参数');
        }
        if ($request->input('password') != $request->input('repassword'))
        {
            return self::response([], 40011, '两次输入密码不相等');
        }
        $user = UserBaseModel::where('user_token',$request->input('token'))->select()->first();
        if (!$user->id)
        {
            return self::response([], 40003, 'token失效,请重新登录');
        }
        $res =  $this->_change_password($user,$request->input('password'));
        return $res ? self::response([],200,'修改密码成功') : self::response([],40000,'修改密码失败');
    }

    /**
     * @param Request $request 获取用户消息列表
     */
    public function get_user_message(Request $request)
    {
        if (!$request->input('uid'))
        {
            return self::response([],40001,'缺少参数uid');
        }
        $data = MessageModel::where('uid',$request->input('uid'))->orwhere('uid',0)->select()->paginate(10)->toArray()['data'];

        return self::response($data);
    }

    public function read_message(Request $request)
    {
        if (!$request->input('uid'))
        {
            return self::response([],40001,'缺少参数uid');
        }
        if (!$request->input('mid'))
        {
            return self::response([],40002,'缺少参数mid');
        }
        $data = MessageModel::where('id',$request->input('mid'))->select()->first();
        if (empty($data))
        {
            return self::response([],40003,'数据库中不存在data');
        }
        $data->is_read = 1;
        $data->save();
        return self::response([]);
    }



}
